import java.util.*;
import java.time.LocalDateTime;
import java.util.UUID;

public class TicketManager {
    private List<Ticket> tickets = new ArrayList<>();
    private ScheduleManager scheduleManager;

    public TicketManager(ScheduleManager scheduleManager) {
        this.scheduleManager = scheduleManager;
    }

    // 票务管理相关方法待实现
    public List<Ticket> getTickets() {
        return tickets;
    }

    // 售票：支持片名+场次、场次编号、选座、连票、儿童票、折扣
    public List<Ticket> sellTickets(String scheduleId, List<int[]> seats, String buyer, List<Boolean> isChildList) {
        Schedule schedule = null;
        for (Schedule s : scheduleManager.getSchedules()) {
            if (s.getScheduleId().equals(scheduleId)) {
                schedule = s;
                break;
            }
        }
        if (schedule == null) throw new IllegalArgumentException("无效的场次编号");
        // 检查座位是否已售出
        for (int i = 0; i < seats.size(); i++) {
            int[] seat = seats.get(i);
            if (isSeatSold(schedule, seat[0], seat[1])) {
                throw new IllegalArgumentException("座位已售出: " + seat[0] + "排" + seat[1] + "座");
            }
        }
        List<Ticket> soldTickets = new ArrayList<>();
        for (int i = 0; i < seats.size(); i++) {
            int[] seat = seats.get(i);
            boolean isChild = isChildList != null && isChildList.size() > i && isChildList.get(i);
            double price = schedule.getPrice();
            // 折扣策略：早上10点场打9折
            if (schedule.getStartTime().getHour() == 10) {
                price *= 0.9;
            }
            // 儿童票策略：身高1.3米及以下或年龄12岁及以下（此处以isChild参数为准），儿童票半价
            if (isChild) {
                price *= 0.5;
            }
            String ticketId = UUID.randomUUID().toString();
            Ticket ticket = new Ticket(ticketId, schedule, seat[0], seat[1], buyer, isChild, price);
            tickets.add(ticket);
            soldTickets.add(ticket);
        }
        return soldTickets;
    }

    // 判断座位是否已售出
    private boolean isSeatSold(Schedule schedule, int row, int seatNumber) {
        for (Ticket t : tickets) {
            if (!t.isRefunded() && t.getSchedule().getScheduleId().equals(schedule.getScheduleId()) && t.getRow() == row && t.getSeatNumber() == seatNumber) {
                return true;
            }
        }
        return false;
    }

    // 退票：电影开场前1小时允许退票，支持退票策略
    public double refundTicket(String ticketId) {
        for (Ticket t : tickets) {
            if (t.getTicketId().equals(ticketId) && !t.isRefunded()) {
                LocalDateTime now = LocalDateTime.now();
                LocalDateTime start = t.getSchedule().getStartTime();
                if (now.isAfter(start.minusHours(1))) {
                    throw new IllegalArgumentException("电影开场前1小时内不允许退票");
                }
                double refundAmount = t.getPrice();
                // 退票策略：提前一天全额退，提前一小时扣10%
                if (now.isAfter(start.minusDays(1))) {
                    refundAmount *= 0.9;
                }
                t.setRefunded(true);
                return refundAmount;
            }
        }
        throw new IllegalArgumentException("无效的电子票号");
    }
} 